package com.dp.iot.admin.api.iotequipment;

import com.dp.iot.core.service.CommonRestController;
import org.springframework.beans.factory.InitializingBean;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.Date;
import java.sql.Time;
import java.sql.Timestamp;
import java.math.*;
import java.util.List;
import java.util.Map;
import com.dp.iot.core.common.constant.PageConstant;
import com.dp.iot.core.common.exception.BusinessException;
import com.dp.iot.core.serialize.ResponseMsg;
import com.dp.iot.model.IotEquipment;
import com.dp.iot.service.iotequipment.service.IotEquipmentService;

import com.dp.iot.core.utils.ListUtil;
import com.dp.iot.core.utils.MapUtil;
import com.dp.iot.core.utils.StringUtil;


/**
* 代码为自动生成 Created by www.magicalcoder.com
* 如果你改变了此类 read 请将此行删除
* 欢迎加入官方QQ群:323237052
*/

@RequestMapping("/admin/iot_equipment_rest/")
@RestController
public class AdminIotEquipmentRestController extends CommonRestController<IotEquipment,String> implements InitializingBean
{

    @Resource
    private IotEquipmentService iotEquipmentService;

    //分页查询
    @RequestMapping(value={"page"}, method={RequestMethod.GET})
    public ResponseMsg page(
        @RequestParam(required = false,value ="equipmentIdFirst")                            String equipmentIdFirst ,
        @RequestParam(required = false,value ="productLineIdFirst")                            String productLineIdFirst ,
        @RequestParam(required = false,value ="equipmentTypeIdFirst")                            String equipmentTypeIdFirst ,
        @RequestParam(required = false,value ="equipmentCodeFirst")                            String equipmentCodeFirst ,
        @RequestParam(required = false,value ="equipmentNameFirst")                            String equipmentNameFirst ,
        @RequestParam(required = false,value ="equipmentStatusFirst")                            String equipmentStatusFirst ,
        @RequestParam(required = false,value ="equipmentIpFirst")                            String equipmentIpFirst ,
        @RequestParam(required = false,value ="equipmentStartTimeFirst")                    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")Date equipmentStartTimeFirst ,
        @RequestParam(required = false,value ="equipmentStartTimeSecond")                    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")Date equipmentStartTimeSecond ,
        @RequestParam(required = false,value ="equipmentEndTimeFirst")                    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")Date equipmentEndTimeFirst ,
        @RequestParam(required = false,value ="equipmentEndTimeSecond")                    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")Date equipmentEndTimeSecond ,
        @RequestParam(required = false,value ="gatewayIdFirst")                            String gatewayIdFirst ,
        @RequestParam(required = false,value ="equipmentImageFirst")                            String equipmentImageFirst ,
        @RequestParam(required = false,value ="equipmentManagerFirst")                            String equipmentManagerFirst ,
        @RequestParam(required = false,value ="equipmentBrandFirst")                            String equipmentBrandFirst ,
        @RequestParam(required = false,value ="equipmentSupplierFirst")                            String equipmentSupplierFirst ,
        @RequestParam(required = false,value ="equipmentPriceFirst")                            BigDecimal equipmentPriceFirst ,
        @RequestParam(required = false,value ="remarksFirst")                            String remarksFirst ,
        @RequestParam(required = false,value ="creationTimeFirst")                    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")Date creationTimeFirst ,
        @RequestParam(required = false,value ="creationTimeSecond")                    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")Date creationTimeSecond ,
        @RequestParam(required = false,value ="creationUserFirst")                            String creationUserFirst ,
        @RequestParam(required = false,value ="updateTimeFirst")                    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")Date updateTimeFirst ,
        @RequestParam(required = false,value ="updateTimeSecond")                    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")Date updateTimeSecond ,
        @RequestParam(required = false,value ="updateUserFirst")                            String updateUserFirst ,
        @RequestParam(required = false,value ="spare1First")                            String spare1First ,
        @RequestParam(required = false,value ="spare2First")                            String spare2First ,
        @RequestParam(required = false,value ="spare3First")                            String spare3First ,
        @RequestParam int page,@RequestParam int limit,@RequestParam(required = false) String safeOrderBy)
    {
        limit = Math.min(limit, PageConstant.MAX_LIMIT);
        int start = (page - 1) * limit;
        Map<String,Object> query = new HashedMap();
        query.put("equipmentIdFirst",coverBlankToNull(equipmentIdFirst));
        query.put("productLineIdFirst",coverBlankToNull(productLineIdFirst));
        query.put("equipmentTypeIdFirst",coverBlankToNull(equipmentTypeIdFirst));
        query.put("equipmentCodeFirst",coverBlankToNull(equipmentCodeFirst));
        query.put("equipmentNameFirst",coverBlankToNull(equipmentNameFirst));
        query.put("equipmentStatusFirst",coverBlankToNull(equipmentStatusFirst));
        query.put("equipmentIpFirst",coverBlankToNull(equipmentIpFirst));
        query.put("equipmentStartTimeFirst",equipmentStartTimeFirst);
        query.put("equipmentStartTimeSecond",equipmentStartTimeSecond);
        query.put("equipmentEndTimeFirst",equipmentEndTimeFirst);
        query.put("equipmentEndTimeSecond",equipmentEndTimeSecond);
        query.put("gatewayIdFirst",coverBlankToNull(gatewayIdFirst));
        query.put("equipmentImageFirst",coverBlankToNull(equipmentImageFirst));
        query.put("equipmentManagerFirst",coverBlankToNull(equipmentManagerFirst));
        query.put("equipmentBrandFirst",coverBlankToNull(equipmentBrandFirst));
        query.put("equipmentSupplierFirst",coverBlankToNull(equipmentSupplierFirst));
        query.put("equipmentPriceFirst",equipmentPriceFirst);
        query.put("remarksFirst",coverBlankToNull(remarksFirst));
        query.put("creationTimeFirst",creationTimeFirst);
        query.put("creationTimeSecond",creationTimeSecond);
        query.put("creationUserFirst",coverBlankToNull(creationUserFirst));
        query.put("updateTimeFirst",updateTimeFirst);
        query.put("updateTimeSecond",updateTimeSecond);
        query.put("updateUserFirst",coverBlankToNull(updateUserFirst));
        query.put("spare1First",coverBlankToNull(spare1First));
        query.put("spare2First",coverBlankToNull(spare2First));
        query.put("spare3First",coverBlankToNull(spare3First));
        Integer count = iotEquipmentService.getModelListCount(query);
        query.put("start",start);
        query.put("limit",limit);
        if(StringUtil.isBlank(safeOrderBy)){
            query.put("notSafeOrderBy","equipment_id desc");
        }else{
            query.put("safeOrderBy",safeOrderBy);
        }
        return new ResponseMsg(count,iotEquipmentService.getModelList(query));
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        super.commonService = iotEquipmentService;
        super.primaryKey = "equipmentId";//硬编码此实体的主键名称
    }
}
